/**
 * @(#) MedicalMoneyProxy.as
 *
 * @date		2011. 03. 25
 * @version		1.0
 * @author		baek kwang hyoun
 * @description	건강보험증 Proxy - Proxy
 * 				건강보험증를 조회를한다.
 */
package com.skt.common_hr.biztrip2.model
{
	import com.skt.common_hr.biztrip2.model.vo.BizTripListVO;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class Biztrip2Proxy extends CommonProxy
	{
		public static const NAME:String = "HthIcProxy";

		public static const LOAD_MEDICALMONEY_LIST_COMPLETE:String	= "LoadMedicalMoneyListComplete";
		public static const LOAD_MEDICALMONEY_LIST_FAILED:String	= "LoadMedicalMoneyListFailed";


		public var mainList:ArrayCollection;
		public var subTabList:ArrayCollection;
		public var acOitab:ArrayCollection;

		public var key:String;

		//지원기준
		public var loadFaultMessage:String;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function Biztrip2Proxy(data:Object=null)
		{
			super(NAME, data);
		}  

		// 출장현황 리스트 호출..
		public function loadBizTrip2List(pernr:String, myOrgCode:String, df1:Date, df2:Date, 
										TrStatus:Object, TrType:Object, txtName:String,
										I_PERNR:String, setFunc:Function):void
		{
			var sTrStatus:String;
			var sTrType:String;
			if(TrStatus) {
				sTrStatus = TrStatus.CODE;
			} else {
				sTrStatus = '';
			}
			if(TrType) {
				sTrType	= TrType.CODE;
			} else {
				sTrType = '';
			}			
			 
			var params:Object = new Object();
			params["I_ORGEH"] = myOrgCode;
			params["I_BEGDA"] = DateUtil.toBasicString(df1);
			params["I_ENDDA"] = DateUtil.toBasicString(df2);
			params["I_TRTYPE"] = sTrType;
			params["I_TRSTATUS"] = sTrStatus;
			params["I_SEARCH"] = txtName;
			params["I_PERNR"] = I_PERNR;
			
			loadData("Z_HR_T022", params, loadBizTrip2ListResult, listFault, setFunc);
		}
		public function loadBizTrip2ListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				mainList = parseList(event.result.RTAB);
				event.result.func(mainList)
			}
			else {
				Alert.show('loadListResult err: ' + retText);
			}
		}

		// 전표출력..
		public function loadPrint(key:String, setFunc:Function):void
		{			
			var params:Object = new Object();
			params["I_GWAREKEY02"] = key;
			loadData("Z_HR_T017", params, loadPrintResult, listFault, setFunc);
		}
		public function loadPrintResult(event:ResultEvent):void
		{			
			var url:String;
			if(event.result["RETCODE"]==0) {  
				url = StringUtil.NVL(event.result["O_URL"]);
				event.result.func(url)
			}
			else {
				trace( "failed to 정산전표 URL");
				url = "";
			}
		}

			

		private function parseMainList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var obj:Object in recArr){
				//var retObj:MedicalSubTabVO = new MedicalSubTabVO();
				var retObj:Object = new Object();
				for(var field:String in obj) {
					if(field == "REQDT" || field == "SENDT" ) {
						retObj[field+'_DT'] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field+'_DT']);
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field+'_DT']);
						retObj[field] = obj[field];
					}
					else if(field == "BETRG") {
						retObj[field] = Math.round(obj[field]*100);
					}
					else {
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}


		private function toJsonType(vo:Object):Object {
			var retObj:Object = new Object;
			for(var field:String in vo) {
				if(vo[field] is Date) {
					retObj[field] = DateUtil.toBasicString(vo[field]);
				}
				else {
					retObj[field] = vo[field];
				}
				trace(retObj[field]);
			}
			return retObj;
		}

		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}

		private function parseList(srcArr:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			for each(var rec:Object in srcArr) 
			{
				var vo:BizTripListVO = new BizTripListVO();
				vo.GWAREKEY01	= rec["GWAREKEY01"];
				vo.GWAREKEY02	= rec["GWAREKEY02"];
				vo.TRSTATUS		= rec["TRSTATUS"];
				vo.TRSTATX		= rec["TRSTATX"];
				vo.TRTYPE		= rec["TRTYPE"];
				vo.TTEXT		= rec["TTEXT"];
				vo.BEGDA		= DateUtil.stringToDateTime(rec["BEGDA"]);
				vo.ENDDA		= DateUtil.stringToDateTime(rec["ENDDA"]);
				vo.TRTITLE		= rec["TRTITLE"];

				vo.ENAME		= rec["ENAME"];
				vo.BUDAT		= rec["BUDAT"];
				vo.PERNR		= rec["PERNR"];												

				list.addItem(vo);
			}
			return list;
		}	
	}
}